package com.osce.score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.score.ScoreExExamPlace;
import com.doudou.core.container.ListUtil;
import com.osce.interfaces.score.IScoreExExamPlaceService;
import com.osce.interfaces.score.IScoreSheetService;
import com.osce.score.mapper.ScoreExExamPlaceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 考试考场Service业务层处理
 *
 * @author zzx
 * @since 2023-09-05
 */
@Service
public class ScoreExExamPlaceServiceImpl extends ServiceImpl<ScoreExExamPlaceMapper, ScoreExExamPlace> implements IScoreExExamPlaceService {

    @Resource
    IScoreSheetService sheetService;

    /**
     * 查询考试考场
     *
     * @param id 考试考场主键
     * @return 考试考场
     */
    @Override
    public ScoreExExamPlace selectScoreExExamPlaceById(Long id) {
        return baseMapper.selectScoreExExamPlaceById(id);
    }

    /**
     * 查询考试考场列表
     *
     * @param scoreExExamPlace 考试考场
     * @return 考试考场
     */
    @Override
    public List<ScoreExExamPlace> selectScoreExExamPlaceList(ScoreExExamPlace scoreExExamPlace) {
        return baseMapper.selectScoreExExamPlaceList(scoreExExamPlace);
    }

    /**
     * 新增考试考场
     *
     * @param scoreExExamPlace 考试考场
     * @return 结果
     */
    @Override
    public int insertScoreExExamPlace(ScoreExExamPlace scoreExExamPlace) {
        return baseMapper.insertScoreExExamPlace(scoreExExamPlace);
    }

    /**
     * 修改考试考场
     *
     * @param scoreExExamPlace 考试考场
     * @return 结果
     */
    @Override
    public int updateScoreExExamPlace(ScoreExExamPlace scoreExExamPlace) {
        return baseMapper.updateScoreExExamPlace(scoreExExamPlace);
    }

    /**
     * 批量删除考试考场
     *
     * @param ids 需要删除的考试考场主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamPlaceByIds(Long[] ids) {
        return baseMapper.deleteScoreExExamPlaceByIds(ids);
    }

    /**
     * 删除考试考场信息
     *
     * @param id 考试考场主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamPlaceById(Long id) {
        return baseMapper.deleteScoreExExamPlaceById(id);
    }

    @Override
    public int batchInsertScoreExExamPlace(List<ScoreExExamPlace> scoreExExamPlaces, Long examId) {
        if (ListUtil.isEmpty(scoreExExamPlaces)) {
            return 0;
        }
        scoreExExamPlaces = scoreExExamPlaces.stream().peek(place -> place.setExamId(examId)).collect(Collectors.toList());
        return baseMapper.batchInsertScoreExExamPlace(scoreExExamPlaces);
    }

    @Override
    public int deleteScoreExExamPlaceByExamIds(Long[] ids) {
        return baseMapper.deleteScoreExExamPlaceByExamIds(ids);
    }

    @Override
    public int deleteScoreExExamPlaceByExamId(Long id) {
        return baseMapper.deleteScoreExExamPlaceByExamId(id);
    }

    @Override
    public Map<Long, ScoreExExamPlace> selectBasicExPlaceToMapByIds(Long[] ids) {
        Map<Long, ScoreExExamPlace> placeMap = baseMapper.selectBasicExPlaceToMapByIds(ids);
        placeMap.forEach((id, place) -> {
            place.setSheetsStr(sheetService.selectScoreSheetStrByIds(place.getSkillId()));
        });
        return placeMap;
    }
}
